/*
 * Copyright (C) Antonio Ramon Morcillo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package soft.xitxarello.su.data;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;

/**
 * This class holds the different groups of android permissions.
 */
public class PermsGroup {

    // Group name.
    private String groupName;

    // Group permissions name.
    private List<String> permissionsNames;

    /**
     * Default constructor.
     * 
     * @param context Interface to global information about an application
     *            environment.
     * @param permInfo Information you can retrieve about a particular security
     *            permission known to the system.
     */
    public PermsGroup(Context context, PermissionGroupInfo permGroupInfo) {
        this.groupName = (String) permGroupInfo.loadLabel(context.getPackageManager());
        this.permissionsNames = new ArrayList<String>();
    }

    /**
     * Add new element to permissions list of the group.
     * 
     * @param permInfo Information you can retrieve about a particular security
     *            permission known to the system.
     */
    public void addPermission(Context context, PermissionInfo permInfo) {

        if (!permissionsNames.contains(permInfo.loadLabel(context.getPackageManager()))) {
            permissionsNames.add((String) permInfo.loadLabel(context.getPackageManager()));
        }
    }

    /**
     * Getter Method.
     * 
     * @return the name of the group.
     */
    public String getGroupName() {
        return groupName;
    }

    /**
     * Getter Method.
     * 
     * @return a list with permissions names that are in te group.
     */
    public List<String> getPermissionsNames() {
        return permissionsNames;
    }

    /**
     * Overrides a superclass method. Compares this instance with the specified
     * object and indicates if they are equal.
     */
    @Override
    public boolean equals(Object o) {
        PermsGroup tmpPermsGroup = (PermsGroup) o;
        return (tmpPermsGroup.getGroupName()).equals(this.groupName);
    }

}
